package org.aldar.bquest.util;

import org.aldar.bquest.resources.ResourceManager;
import org.aldar.bquest.screens.MainBoardScreen;
import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.shape.IShape;
import org.andengine.entity.sprite.Sprite;
import org.andengine.util.modifier.IModifier;

import javax.microedition.khronos.opengles.GL10;

/**
 * User: Daria
 * Date: 9/5/14 10:57 PM
 */
public class SpriteUtil {

    public static Entity getMoveHeroSymbolSprite() {
        //Rectangle moveSymbol = new Rectangle(0,0,50,50, MainBoardScreen.getScm().getVertexBufferObjectManager());
        //moveSymbol.setColor(Color.YELLOW);
        Sprite moveSymbol = new Sprite(0, 0, ResourceManager.getInstance().getSymbolGoRegion(), MainBoardScreen.getScm().getVertexBufferObjectManager());
        return moveSymbol;
    }

    public static void revealEntityAndChildren(float duration, float fromAlpha, float toAlpha, IEntity entity, final Runnable runnable) {
        revealEntityAndChildren(duration, fromAlpha, toAlpha, entity, runnable, true);
    }

    public static void revealEntityAndChildren(float duration, float fromAlpha, float toAlpha, IEntity entity, final Runnable runnable, boolean applyToSelf) {
        if (entity instanceof IShape) {
            ((IShape) entity).setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        }
        entity.setAlpha(fromAlpha);
        if (applyToSelf) {
            entity.registerEntityModifier(new AlphaModifier(duration, fromAlpha, toAlpha, new IEntityModifier.IEntityModifierListener() {
                @Override
                public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
                }

                @Override
                public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                    if (runnable != null) {
                        runnable.run();
                    }
                }
            }));
        }
        for (int i = 0; i < entity.getChildCount(); i++) {
            if (i == 0 && !applyToSelf && runnable != null) {
                revealEntityAndChildren(duration, fromAlpha, toAlpha, entity.getChildByIndex(i), runnable);
            } else {
                revealEntityAndChildren(duration, fromAlpha, toAlpha, entity.getChildByIndex(i), null);
            }
        }

    }

}
